<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Collect extends Stourweb_Controller
{
    private static $_member;
    public function before()
    {
        parent::before();
        foreach (array($_REQUEST, $_POST, $_GET) as $items) {
            foreach ($items as &$param) {
                $param = Common::remove_xss($param);
            }
        }
        self::$_member = Model_Member_Login::check_login_info();
    }

    //已收藏
    public static function is_exist($data)
    {
        $member = Model_Member_Login::check_login_info();
        if (!$member) {
            return false;
        }
        $r = DB::select()->from('product_collect')->where('member_id', '=', $member['mid'])->and_where('type_id', '=', $data['type_id'])->and_where('auto_id', '=', $data['id'])->execute()->current();
        return $r ? true : false;
    }

    //更改收藏
    public function action_modify()
    {
        $data = &$_POST;
        if (!self::$_member || !$data['type_id'] || !$data['type_id']) {
            exit(json_encode(array('status' => 0)));
        }

        $r = DB::select()->from('product_collect')->where('member_id', '=', self::$_member['mid'])->and_where('type_id', '=', $data['type_id'])->and_where('auto_id', '=', $data['id'])->execute()->current();
        if ($r) {
            DB::delete('product_collect')->where('id', '=', $r['id'])->execute();
        } else {
            $i = array(
                'type_id' => $data['type_id'],
                'auto_id' => $data['id'],
                'member_id' => self::$_member['mid'],
            );
            DB::insert('product_collect', array_keys($i))->values(array_values($i))->execute();
        }
        echo json_encode(array('status' => 1));
    }

    //会员中心
    public function action_member()
    {
        $backUrl = 'href="javascript:;" onclick="javascript:history.go(-1);"';
        if (stripos($_SERVER['HTTP_REFERER'], Common::cookie_domain()) === false) {
            $conf = require dirname(DOCROOT) . '/data/mobile.php';
            $url = Common::get_main_host() . '/phone/';
            if (stripos($conf['domain']['mobile'], $url) === false) {
                $url = $conf['domain']['mobile'];
            }
            $backUrl = 'href="' . $url . '"';
        }
        $this->assign('backurl', $backUrl);
        $this->display('collect/list');
    }

    //会员中心获取收藏
    public function action_ajax_data()
    {
        $r = array('data' => array());
        $p = (int) $_POST['p'];
        $p = $p ? $p : 1;
        $o = ($p - 1) * 30;
        $r['data'] = DB::select()->from('product_collect')->where('member_id', '=', self::$_member['mid'])->offset($o)->limit(30)->execute()->as_array();
        $models = array();
        $result = DB::select()->from('model')->where('isopen', '=', 1)->execute();
        foreach ($result as $item) {
            $models[$item['id']] = $item;
        }
        foreach ($r['data'] as &$item) {
            $m = $models[$item['type_id']]['maintable'];
            $d = DB::select()->from($m)->where('id', '=', $item['auto_id'])->execute()->current();
            if(in_array($item['type_id'],array(4,101))){
                $item['commentnum'] = Model_Comment::get_comment_num($item['auto_id'], $item['type_id']);
                $item['title'] = $d['title'];
                $item['litpic'] = Common::img($d['litpic'], 160, 160);
                $item['url'] = $item['type_id']==4?Common::get_web_url($d['webid']) . "/raiders/show_{$d['aid']}.html":$GLOBALS['cfg_basehost'] . '/notes/show_' . $d['id'] . '.html';
                $item['model'] = $models[$item['type_id']]['modulename'];
            }else{;
                if ($m != 'guide_service') {
                    $_model = "Model_" . ucfirst($m);
                    $reflectionMethod = new ReflectionMethod($_model, 'get_minprice');
                    $item['price'] = $reflectionMethod->invoke(new $_model(), $d['id'], array('info' => $d));
                    $item['url'] = Common::get_web_url($d['webid']) . "/" . ($models[$item['type_id']]['correct'] ? $models[$item['type_id']]['correct'] : $models[$item['type_id']]['pinyin']) . "/show_{$d['aid']}.html";
                } else {
                    $item['price'] = $d['price'];
                    $item['url'] = Common::get_web_url($d['webid']).'/guide/service/show_' . $d['aid'] . '.html';
                }
                if ($item['type_id'] == 5) {
                    $item['price'] = $item['price']['price'];
                }
                $item['commentnum'] = Model_Comment::get_comment_num($item['auto_id'], $item['type_id']);
                $item['satisfyscore'] = St_Functions::get_satisfy($item['type_id'], $d['id'], $d['satisfyscore']);
                $item['title'] = $d['title'];
                $item['litpic'] = Common::img($d['litpic'], 160, 160);
                $item['model'] = $models[$item['type_id']]['modulename'];
            }
        }

        $r['completed'] = count($r['data']) == 30 ? false : true;
        echo json_encode($r);
    }
    //会员中心取消收藏
    public function action_delete()
    {
        $data = &$_POST['id'];
        if ($data) {
            DB::delete('product_collect')->where('member_id', '=', self::$_member['mid'])->and_where('id', 'in', DB::expr('(' . implode(',', $data) . ')'))->execute();
        }
        echo json_encode(array('status' => 1));
    }

}
